/*
* jlb
*/
package com.jlb.setting.ctrl;

import com.alibaba.fastjson.JSON;
import com.jlb.core.entity.BeanRet;
import com.jlb.core.entity.Page;
import com.jlb.core.tools.redis.RedisUtils;
import com.jlb.setting.entity.Setting;
import com.jlb.setting.service.SettingSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 设置 控制器
 *
 * @author jlb
 */
@RestController
@RequestMapping("/setting")
@Slf4j
@Api(value = "设置控制器", description = "设置控制器")
public class SettingCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private SettingSV settingSV;

    /**
     * 查询Setting一个详情信息
     *
     * @param id id
     * @param k  键
     * @return BeanRet
     */
    @ApiOperation(value = "查询Setting一个详情信息", notes = "查询Setting一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "k", value = "键", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(java.lang.Long id, java.lang.String k) {
        if (id == null) {
            return null;
        }
        if (k == null) {
            return null;
        }
        Setting setting = settingSV.load(id, k);
        log.info(JSON.toJSONString(setting));
        return BeanRet.create(true, "查询成功", setting);
    }


    /**
     * 根据条件id查询Setting一个详情信息
     *
     * @param id id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Setting一个详情信息", notes = "查询Setting一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable java.lang.Long id) {
        if (id == null) {
            return null;
        }
        Setting setting = settingSV.loadById(id);
        log.info(JSON.toJSONString(setting));
        return BeanRet.create(true, "查询成功", setting);
    }

    /**
     * 根据条件k查询Setting一个详情信息
     *
     * @param k 键
     * @return BeanRet
     */
    @ApiOperation(value = "查询Setting一个详情信息", notes = "查询Setting一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "k", value = "键", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/k/{k}")
    @ResponseBody
    public BeanRet loadByK(@PathVariable java.lang.String k) {
        if (k == null) {
            return null;
        }
        Setting setting = settingSV.loadByK(k);
        log.info(JSON.toJSONString(setting));
        return BeanRet.create(true, "查询成功", setting);
    }


    /**
     * 查询Setting信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Setting信息集合", notes = "查询Setting信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore Setting setting, Integer curPage, Integer pageSize) {
        Page<Setting> page = new Page<Setting>(pageSize, curPage);
        List<Setting> settings = settingSV.list(setting, page.genRowStart(), page.getPageSize());
        page.setVoList(settings);
        int total = settingSV.count(setting);
        page.setTotalRow(total);

        return BeanRet.create(true, "查询成功", page);

    }


    /**
     * 查询Setting信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Setting信息集合", notes = "查询Setting信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "k", value = "键", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(java.lang.Long id, java.lang.String k, Integer curPage, Integer pageSize) {
        Page<Setting> page = new Page<Setting>(pageSize, curPage);
        List<Setting> settings = settingSV.list(id, k, page.genRowStart(), page.getPageSize());
        page.setVoList(settings);
        int total = settingSV.count(id, k);
        page.setTotalRow(total);

        return BeanRet.create(true, "查询成功", page);
    }

    /**
     * 统计Setting信息数量根据主键
     *
     * @return 总条数
     */
    @ApiOperation(value = " 统计Setting信息数量根据主键", notes = " 统计Setting信息数量根据主键")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "k", value = "键", paramType = "query")
    })
    @GetMapping(value = "/count/by")
    @ResponseBody
    public Integer count(java.lang.Long id, java.lang.String k) {
        return settingSV.count(id, k);
    }

    /**
     * 统计Setting信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计Setting信息数量", notes = "统计Setting信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "k", value = "键", paramType = "query"),
            @ApiImplicitParam(name = "v", value = "值", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "备注 128个汉字", paramType = "query"),
            @ApiImplicitParam(name = "ver", value = "版本", paramType = "query")
    })
    @GetMapping(value = "/count")
    @ResponseBody
    public Integer count(@ApiIgnore Setting setting) {
        if (setting == null) {
            return settingSV.count(new HashMap());
        } else {
            return settingSV.count(setting);
        }
    }


    /**
     * 创建Setting
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建Setting", notes = "创建Setting")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "v", value = "值", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "备注 128个汉字", paramType = "query"),
            @ApiImplicitParam(name = "ver", value = "版本", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore Setting setting) {
        settingSV.save(setting);
        return BeanRet.create(true, "创建成功", setting);
    }


    /**
     * 修改Setting
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改Setting", notes = "修改Setting")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "k", value = "键", paramType = "query"),
            @ApiImplicitParam(name = "v", value = "值", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "备注 128个汉字", paramType = "query"),
            @ApiImplicitParam(name = "ver", value = "版本", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore Setting setting) {
        settingSV.update(setting);
        return BeanRet.create(true, "修改成功", setting);
    }

    /**
     * 删除Setting
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除Setting", notes = "删除Setting")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "k", value = "键", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(java.lang.Long id, java.lang.String k) {
        settingSV.delete(id, k);
        return BeanRet.create(true, "删除Setting成功");
    }

}
